package com.yulan.bankback.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.yulan.bankback.entity.result.RetResult;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

@Component
public class AliYunOss{

    private static String endPoint = "https://oss-cn-beijing.aliyuncs.com";

    private static String accessKeyId = "LTAI4FcG61aVXq8uCt1qUE95";
    private static String accessKeySecret = "tdIz6Ft85nshNMImWYr8jDNec8Vxz7";
    private static String bucketName = "yulango-user-img";



    public String upload(MultipartFile file) throws IOException {
        try{
            OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);

            InputStream inputStream = file.getInputStream();

            String fileName=file.getOriginalFilename();

            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            fileName=uuid+fileName;
            String filePath = new Date().toString();
            fileName=filePath+"/"+fileName;
            ossClient.putObject(bucketName, fileName, inputStream);
            ossClient.shutdown();
            return "https://"+bucketName+"."+ endPoint +"/"+fileName;
        }catch (IOException e) {
            e.printStackTrace();
        }
        return "上传失败";
    }


}













